﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Tintuc : System.Web.UI.Page
{
    BUS_News objN = new BUS_News();
    int vitri; //Luu vi tri trang hien hanh
    PagedDataSource page; //Doi tuong dung de luu nguon du lieu phan trang
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["vt"] = 0; //Bien luu tru lai vi tri hien hanh
        }

        vitri = (int)ViewState["vt"];
        DsTintuc();  
    }

    public void DsTintuc()
    {
        int type = 0;
        if (int.TryParse(Request.QueryString.Get("type"), out type))
        {
            //Khoi tao doi tuong nguon phan trang
            page = new PagedDataSource();
            //Gan nguon du lieu vao doi tuong phan trang
            page.DataSource = objN.BUS_News_getDataByType(type).DefaultView;
            //Thiet lap cac thuoc tinh phan trang
            page.PageSize = 9; //PageSize: quy dinh so dong tren moi trang
            page.AllowPaging = true; //Cho phep phan trang        
            page.CurrentPageIndex = vitri; //Trang hien hanh
            linkBtnFirst.Enabled = !page.IsFirstPage;
            linkBtnPrev.Enabled = !page.IsFirstPage;
            linkBtnNext.Enabled = !page.IsLastPage;
            linkBtnLast.Enabled = !page.IsLastPage;
            //An, hien cac button phan trang
            dtlstt.DataSource = page;
            dtlstt.DataBind();
        }
    }

    protected void linkBtnFirst_Click(object sender, EventArgs e)
    {
        vitri = 0;
        ViewState["vt"] = vitri;
        DsTintuc();
    }
    protected void linkBtnLast_Click(object sender, EventArgs e)
    {
        vitri = page.PageCount - 1;
        ViewState["vt"] = vitri;
        DsTintuc();
    }
    protected void linkBtnPrev_Click(object sender, EventArgs e)
    {
        vitri = (int)ViewState["vt"] - 1;
        ViewState["vt"] = vitri;
        DsTintuc();
    }
    protected void linkBtnNext_Click(object sender, EventArgs e)
    {
        vitri = (int)ViewState["vt"] + 1;
        ViewState["vt"] = vitri;
        DsTintuc();
    }
}